package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhidao.model.po.Admin;
import com.example.zhidao.model.po.Announcement;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.service.AdminService;
import com.example.zhidao.service.AnnouncementService;
import com.example.zhidao.utils.Ujwt;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("announcement")
public class AnnouncementController {//6
    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private Ujwt ujwt;


    //    获取所有公告(并按照最新往下降序)
    @GetMapping
    public ResponseData selann() {
        QueryWrapper<Announcement> qw=new QueryWrapper<>();
        qw.orderByDesc("createtime");
        List<Announcement> announcementList = announcementService.list(qw);
        if (announcementList == null) {
            return new ResponseData(announcementList, false, "查询失败", 201);
        } else {
            return new ResponseData(announcementList, true, "查询成功", 200);
        }
    }

    //    获取单条最新的公告
    @GetMapping("/new")
    public ResponseData selnews() {
        QueryWrapper<Announcement> qw=new QueryWrapper<>();
        qw.orderByDesc("createtime");
        qw.last("limit 1");
        List<Announcement> announcementList = announcementService.list(qw);
        if (announcementList == null) {
            return new ResponseData(announcementList, false, "查询失败", 201);
        } else {
            return new ResponseData(announcementList, true, "查询成功", 200);
        }
    }






//    ----------------------------------------------------------------------------------------------------------
//    以下为管理员的接口

    @GetMapping("/admin/getAnnouncement")
    public ResponseData getAnnouncement(String index, String index2, Page p){

//        第一个参数为1则代表updatetime  为2则代表createtime
//        第二个参数为2则代表asc升序，为2则代表desc降序


        QueryWrapper<Announcement> qw=new QueryWrapper<>();
        if(index.equals("1")){
            if(index2.equals("1")){
               qw.orderByAsc("updatetime");
            }else{
                qw.orderByDesc("updatetime");
            }
        }else{
            if(index2.equals("1")){
                qw.orderByAsc("createtime");
            }else{
                qw.orderByDesc("createtime");
            }
        }
        Page<Announcement> announcementList=announcementService.page(p,qw);

        if(announcementList==null){
            return new ResponseData(null,false,"公告获取失败",200);
        }else{
            return new ResponseData(announcementList,true,"获取成功",200);
        }

    }


    //    删除公告
    @DeleteMapping("/admin/del")
    public ResponseData delann(@RequestHeader String token,String id) {

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }


        boolean i = announcementService.removeById(id);
        if (i) {
            return new ResponseData(i, true, "删除成功", 200);
        } else {
            return new ResponseData(i, false, "删除失败", 201);
        }
    }

    //    修改公告
    @PutMapping("/admin/update")
    public ResponseData update(@RequestHeader String token,@RequestBody Announcement announcement) {
        System.out.println(announcement);

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }


        UpdateWrapper<Announcement> uw = new UpdateWrapper<>();
        uw.eq("id", announcement.getId());
        uw.set("content", announcement.getContent());
        Date date = new Date();
        java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
        uw.set("updatetime", timestamp);
        boolean i = announcementService.update(uw);
        if (i) {
            return new ResponseData(i, true, "修改成功", 200);
        } else {
            return new ResponseData(i, false, "修改失败", 201);
        }
    }


    //    发布公告
    @PostMapping("/admin/add")
    public ResponseData insertann(@RequestHeader String token,String content) {

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }


        Announcement ann = new Announcement();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");//自动生成uuid
        ann.setId(uuid);
        ann.setContent(content);

        Date date = new Date();
        java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());

        ann.setCreatetime(timestamp);
        ann.setUpdatetime(timestamp);

        boolean i = announcementService.save(ann);

        System.out.println(ann);

        if (i) {
            return new ResponseData(i, true, "发布成功", 200);
        } else {
            return new ResponseData(i, false, "发布失败", 201);
        }
    }



}
